<script>
import http_config from "@/config/http";

export default {
  name: "uploadVideo",
  props: {

  },
  data(){
    return {
      baseApiUrl: http_config.base_url,
      uploadNum:1
    }
  },
  methods: {

    chooseVideo(){
      var that = this;
      let user = uni.getStorageSync("xykeep:user");
      let userToken = user.token;
      var video=[]
      uni.chooseVideo({
        count: this.uploadNum,
        sourceType: ['album', 'camera'],
        compressed:true,
        success: (res) => {
          uni.showLoading({
            title:'视频上传中'
          })
          video.push(res)

          uni.uploadFile({
            url: that.baseApiUrl + '/addons/alioss/index/upload',
            filePath: video[0].tempFilePath,
            fileType: 'video',
            name: 'file',
            headers: {
              'Accept': 'application/json',
              'Content-Type': 'multipart/form-data',
              'token': userToken
            },
            formData: {
              size : video[0].size,
              width: video[0].width,
              height: video[0].height
            },
            success: (uploadFileRes) => {
              uni.hideLoading()
              var dataimg=JSON.parse(uploadFileRes.data);
              if(!dataimg.data.url){
                this.$xyfun.msg('上传失败');
              }else {
                this.$emit("chooseFile",{
                  sourcePath : dataimg.data.url,
                  localPath : video[0].tempFilePath
                })
              }
            },
            fail: (error) => {
              if (error && error.response) {
                this.$common.showError(error.response);
              }
            },
            complete: () => {
              setTimeout(function () {
                uni.hideLoading();
              }, 250);
            },
          });

        },
      })
    }

  }
}
</script>

<template>
  <view class="choose_image" @tap="chooseVideo"></view>
</template>

<style scoped lang="scss">
.choose_image{

}
</style>